home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / SampleTreeModel.java < prev    next >
Text File  |  1998-06-30  |  2KB  |  65 lines

  1. /*
  2.  * @(#)SampleTreeModel.java    1.3 97/09/23
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. import com.sun.java.swing.tree.DefaultTreeModel;
  22. import com.sun.java.swing.tree.TreeNode;
  23. import com.sun.java.swing.tree.TreePath;
  24. import com.sun.java.swing.tree.DefaultMutableTreeNode;
  25. import java.awt.Color;
  26.  
  27. /**
  28.   * SampleTreeModel extends JTreeModel to extends valueForPathChanged.
  29.   * This method is called as a result of the user editing a value in
  30.   * the tree.  If you allow editing in your tree, are using TreeNodes
  31.   * and the user object of the TreeNodes is not a String, then you're going
  32.   * to have to subclass JTreeModel as this example does.
  33.   *
  34.   * @version 1.3 09/23/97
  35.   * @author Scott Violet
  36.   */
  37.  
  38. public class SampleTreeModel extends DefaultTreeModel
  39. {
  40.     /**
  41.       * Creates a new instance of SampleTreeModel with newRoot set
  42.       * to the root of this model.
  43.       */
  44.     public SampleTreeModel(TreeNode newRoot) {
  45.     super(newRoot);
  46.     }
  47.  
  48.     /**
  49.       * Subclassed to message setString() to the changed path item.
  50.       */
  51.     public void valueForPathChanged(TreePath path, Object newValue) {
  52.     /* Update the user object. */
  53.     DefaultMutableTreeNode      aNode = (DefaultMutableTreeNode)path.getLastPathComponent();
  54.     SampleData    sampleData = (SampleData)aNode.getUserObject();
  55.  
  56.     sampleData.setString((String)newValue);
  57.     /* UUUhhhhh, pretty colors. */
  58.     sampleData.setColor(Color.green);
  59.  
  60.     /* Since we've changed how the data is to be displayed, message
  61.        nodeChanged. */
  62.     nodeChanged(aNode);
  63.     }
  64. }
  65.